package com.mall.user.controller;

import com.mall.common.constant.MessageConstant;
import com.mall.common.entity.Result;
import com.mall.common.exception.GlobalException;
import com.mall.common.utils.UserContext;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Validated
public class TestController {

    @PostMapping("/h")
    public Result<?> test2(){
        System.out.println("GlobalExceptionHandler 已生效");
        throw new GlobalException(MessageConstant.LOGIN_FAILED);
    }
    @GetMapping("/hello")
    public Result<String> test(@RequestHeader(value = "id",required = false) String id){
        System.out.println("UserContext.getUser()的值为：" + UserContext.getUser());
        return Result.success(id);
    }
    @PostMapping("/test")
    public String test(@Valid @RequestBody TestDTO dto) {
        System.out.println("🔥 进入 test 方法！phone = " + dto.getName());
        return "success";
    }
    @Data
    public static class TestDTO {
        @NotBlank(message = "名字不能为空")
        private String name;

        // getter/setter
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
    }
}